package com.tarena.lbs.business.controller;

import com.tarena.lbs.base.protocol.exception.BusinessException;
import com.tarena.lbs.base.protocol.model.Result;
import com.tarena.lbs.base.protocol.pager.PageResult;
import com.tarena.lbs.business.pojo.param.ChainQuery;
import com.tarena.lbs.business.pojo.param.StoreParam;
import com.tarena.lbs.business.pojo.param.UserLocationParam;
import com.tarena.lbs.business.pojo.query.AreaStoreQuery;
import com.tarena.lbs.business.pojo.query.StoreQuery;
import com.tarena.lbs.business.pojo.vo.ChainVO;
import com.tarena.lbs.business.pojo.vo.StoreVO;
import com.tarena.lbs.business.servcie.StoreService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@Tag(name = "店铺相关功能接口")
@RestController
@Slf4j
public class StoreController {
    @Autowired
    private StoreService storeService;


    @Operation(summary = "查询店铺列表")
    @PostMapping("/admin/basic/store/getAll")
    public Result<PageResult<StoreVO>> pageList(@RequestBody StoreQuery query)
        throws BusinessException {
        log.info("查询店铺列表，入参:{}",query);
        PageResult<StoreVO> pageResult = storeService.pageList(query);
        log.info("查询店铺列表，出参:{}",pageResult.getObjects());
        return new Result<>(pageResult);
    }

    /**
     * 新增店铺
     * @param param
     * @return
     * @throws BusinessException
     */
    @Operation(summary = "新增店铺")
    @PostMapping("/admin/basic/store/add")
    public Result<Void> save(@RequestBody StoreParam param)
        throws BusinessException {
        log.info("新增店铺，入参:{}",param);
        storeService.save(param);
        return Result.success();
    }
    @Operation(summary = "更新店铺为启用状态")
    @PutMapping("/admin/basic/store/enable/{id}")
    public Result<Void> updateStatus(@PathVariable("id") Integer id)
        throws BusinessException {
        log.info("更新店铺状态，入参:{}",id);
        
        // 检查ID是否为null或无效值
        if (id == null || id <= 0) {
            log.warn("启用店铺时ID参数无效: {}", id);
            throw new BusinessException("400", "店铺ID不能为空或无效");
        }
        
        storeService.updateStatus(id);
        return Result.success();
    }
    @Operation(summary = "更新店铺为禁用状态")
    @PutMapping("/admin/basic/store/disable/{id}")
    public Result<Void> disable(@PathVariable("id") Integer id)
        throws BusinessException {
        log.info("更新店铺状态，入参:{}",id);
        
        // 检查ID是否为null或无效值
        if (id == null || id <= 0) {
            log.warn("禁用店铺时ID参数无效: {}", id);
            throw new BusinessException("400", "店铺ID不能为空或无效");
        }
        
        storeService.updateStatusDisable(id);
        return Result.success();
    }
    @Operation(summary = "编辑店铺按钮")
    @GetMapping("/admin/basic/store/{id}")
    public Result<StoreVO> getById(@PathVariable("id") Integer id)
        throws BusinessException {
        log.info("根据id查询店铺，入参:{}",id);
        
        // 检查ID是否为null或无效值
        if (id == null || id <= 0) {
            log.warn("查询店铺时ID参数无效: {}", id);
            throw new BusinessException("400", "店铺ID不能为空或无效");
        }
        
        try {
            StoreVO storeVO = storeService.getStoreById(id);
            log.info("根据id查询店铺，出参:{}",storeVO);
            return new Result<>(storeVO);
        } catch (Exception e) {
            log.error("查询店铺详情时发生异常，店铺ID: {}", id, e);
            throw new BusinessException("500", "查询店铺详情失败");
        }
    }
    @Operation(summary = "更新店铺信息")
    @PutMapping("/admin/basic/store/update")
    public Result<Void> update(@RequestBody StoreParam param)
        throws BusinessException {
        log.info("更新店铺信息，入参:{}",param);
        storeService.save(param);
        return Result.success();
    }
    @Operation(summary = "删除店铺")
    @DeleteMapping("/admin/basic/store/delete/{id}")
    public Result<Void> delete(@PathVariable("id") Integer id)
        throws BusinessException {
        log.info("删除店铺，入参:{}",id);
        
        // 检查ID是否为null或无效值
        if (id == null || id <= 0) {
            log.warn("删除店铺时ID参数无效: {}", id);
            throw new BusinessException("400", "店铺ID不能为空或无效");
        }
        
        storeService.deleteStore(id);
        return Result.success();
    }
    //查询 属于当前登录用户 所属商家 和满足区域条件的店铺列表
    @Operation(summary = "查询区域下的店铺列表")
    @PostMapping("/admin/basic/store/getStoreByCity")
    public Result<PageResult<StoreVO>> getStoreByCity(@RequestBody AreaStoreQuery query)
        throws BusinessException {
        log.info("查询区域下的店铺列表，入参:{}",query);
        PageResult<StoreVO> pageResult = storeService.getStoreByCity(query);
        log.info("查询区域下的店铺列表，出参:{}",pageResult.getObjects());
        return new Result<>(pageResult);
    }
    //手机小程序定位上报
    @Operation(summary = "手机小程序定位上报")
    @PostMapping("/basic/store/location")
    public Result<Void> location(@RequestBody UserLocationParam param)
        throws BusinessException{
        return Result.success();
    }
    @Operation(summary = "查询二维码")
    @PostMapping("/admin/basic/chain/getAll")
    public Result<PageResult<ChainVO>> getAll(@RequestBody ChainQuery query)
        throws BusinessException {
        log.info("查询所有连锁店，入参:{}",query);
        PageResult<ChainVO> pageResult = storeService.getAllChain(query);
        if (pageResult != null) {
            log.info("查询所有连锁店，出参:{}",pageResult.getObjects());
        } else {
            log.info("查询所有连锁店，出参: null");
        }
        return new Result<>(pageResult);
    }
    @Operation(summary = "修改二维码的状态")
    @PutMapping("/admin/basic/chain/enable/{id}")
    public Result<Void> enable(@PathVariable("id") Integer id)
        throws BusinessException {
        log.info("修改二维码的状态，入参:{}",id);
        storeService.enable(id);
        log.info("修改二维码的状态，出参:{}",id);
        return Result.success();
    }
}
